package com.base.api.bds.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.base.api.bds.BdsClinicService;
import com.base.dao.bds.BdsClinicItemListMapper;
import com.base.dao.bds.BdsCodeTableItemMapper;
import com.base.dao.bds.BdsCodeTableMapper;
import com.base.dao.bds.BdsOrganizationMapper;
import com.base.utils.TreeUtil;
import com.base.vo.bds.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@CacheConfig(cacheNames = {"bds"})
public class BdsClinicServiceImpl implements BdsClinicService {
    @Autowired
    private BdsClinicItemListMapper clinicItemListMapper;

    @Autowired
    private BdsOrganizationMapper organizationMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private BdsCodeTableMapper codeTableMapper;

    @Autowired
    private BdsCodeTableItemMapper codeTableItemMapper;

    /**
     * 分页匹配查询诊疗项目
     *
     * @param bdsClinicPageHelper
     * @return
     */
    @Override
    public String getBdsClinicItemListByInputCode(BdsClinicPageHelper bdsClinicPageHelper) {
        PageHelper.startPage(bdsClinicPageHelper.getPage(), bdsClinicPageHelper.getLimit());
        List<BdsClinicItemList> list = clinicItemListMapper.selectByInputCode(bdsClinicPageHelper.getInputCode());
        PageInfo<BdsClinicItemList> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        map.put("code", 0);
        map.put("msg", "");
        return JSON.toJSONString(map);
    }

    /**
     * 获取组织机构
     *
     * @return
     */
    @Cacheable
    @Override
    public String getBdsOrganization() {
        List<BdsOrganization> list = organizationMapper.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        return JSON.toJSONString(map);
    }

    /**
     * 获取组织机构树形JSON
     *
     * @return
     */
    @Cacheable
    @Override
    public String getBdsOrganizationTreeJson() {
        List<BdsOrganization> list = organizationMapper.selectAll();
        JSONArray jsonArray = TreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(list)), "id", "parentId", "children");
        return jsonArray.toJSONString();
    }

    /**
     * 保存组织机构
     *
     * @param bdsOrganization
     * @return
     */
    @CacheEvict(allEntries = true)
    @Override
    public String saveBdsOrganization(BdsOrganization bdsOrganization) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        if (bdsOrganization != null) {
            if (bdsOrganization.getParentId() == null) {
                bdsOrganization.setParentId(0);
            }
            if (bdsOrganization.getId() == null || "".equals(bdsOrganization.getId())) {
                bdsOrganization.setId(organizationMapper.getsquence());
                organizationMapper.insertSelective(bdsOrganization);
            } else {
                organizationMapper.updateByPrimaryKeySelective(bdsOrganization);
            }
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除组织机构
     *
     * @param id
     * @return
     */
    @CacheEvict(allEntries = true)
    @Override
    public String delBdsOrganization(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        if (organizationMapper.hashNext(id) > 0) {
            map.put("code", -1);
            map.put("msg", "当前组织机构存在下级目录，不允许删除。");
        } else {
            organizationMapper.deleteByPrimaryKey(id);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询码表目录
     *
     * @return
     */
    @Cacheable
    @Override
    public String getBdsCodeTable(String keyWords) {
//        if(stringRedisTemplate.hasKey("bds_bdsCodeTable")){
//            return stringRedisTemplate.opsForValue().get("bds_bdsCodeTable");
//        }
        Map<String, Object> map = new HashMap<>();
        List<BdsCodeTable> list = codeTableMapper.selectBykeyWords(keyWords);
        map.put("code", 0);
        map.put("data", list);
//        stringRedisTemplate.opsForValue().set("bds_bdsCodeTable",JSON.toJSONString(map));
        return JSON.toJSONString(map);
    }

    /**
     * 查询码表详情
     *
     * @param tId 主表ID
     * @return
     */
    @Override
    public String getBdsCodeTableItem(Integer tId, String keyWords) {
        Map<String, Object> map = new HashMap<>();
        List<BdsCodeTableItem> list = new ArrayList<>();
        map.put("code", 0);
        if (tId != null) {
            list = codeTableItemMapper.selectByTid(tId, keyWords);
        }
        map.put("data", list);
        return JSON.toJSONString(map);
    }

    /**
     * 禁用启用
     *
     * @param bdsCodeTableItem
     * @return
     */
    @Override
    public String enabledTableItem(BdsCodeTableItem bdsCodeTableItem) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        codeTableItemMapper.updateByPrimaryKeySelective(bdsCodeTableItem);
        return JSON.toJSONString(map);
    }

    /**
     * 保存码表目录
     *
     * @param bdsCodeTable
     * @return
     */
    @CacheEvict(allEntries = true)
    @Override
    public String saveBdsCodeTable(BdsCodeTable bdsCodeTable) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        if (bdsCodeTable != null) {
            if (bdsCodeTable.getId() == null) {
                bdsCodeTable.setId(codeTableMapper.getSquence());
                codeTableMapper.insertSelective(bdsCodeTable);
            } else {
                codeTableMapper.updateSelective(bdsCodeTable);
            }
        }
        return JSON.toJSONString(map);
    }

    /**
     * 保存码表条目
     *
     * @param bdsCodeTableItem
     * @return
     */
    @Override
    public String saveBdsCodeTableItem(BdsCodeTableItem bdsCodeTableItem) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        if (bdsCodeTableItem != null) {
            if (bdsCodeTableItem.getId() == null) {
                bdsCodeTableItem.setId(codeTableItemMapper.getSquence());
                codeTableItemMapper.insertSelective(bdsCodeTableItem);
            } else {
                codeTableItemMapper.updateByPrimaryKey(bdsCodeTableItem);
            }
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除条目
     *
     * @param id
     * @return
     */
    @Override
    public String delCodeTableById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        if (codeTableItemMapper.hashNext(id) > 0) {
            map.put("code", -1);
            map.put("msg", "当前目录存在字典值，不允许删除！");
        } else {
            codeTableMapper.deleteByPrimaryKey(id);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除码表明细
     *
     * @param id
     * @return
     */
    @Override
    public String delCodeTableItemById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        codeTableItemMapper.deleteByPrimaryKey(id);
        return JSON.toJSONString(map);
    }

    /**
     * 根据标准编码获取字典
     *
     * @param standardCode
     * @return
     */
    @Cacheable
    @Override
    public String getTableCodeItemByStandardCode(String standardCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        List<BdsCodeTableItem> list = codeTableItemMapper.selectByStandardCode(standardCode);
        map.put("data", list);
        return JSON.toJSONString(map);
    }

    /**
     * 关键字查询组织机构
     *
     * @param keywords
     * @return
     */
    @Override
    public String getBdsOrganizationByKeyWords(String keywords) {
        List<BdsOrganization> list = organizationMapper.selectByKeywords(keywords);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        return JSON.toJSONString(map);
    }

    /**
     * 查询存在人员信息中的科室
     *
     * @return
     */
    @Cacheable
    @Override
    public Map<String, Object> getBdsOrganizationInUum() {
        List<BdsOrganization> list = organizationMapper.selectByUum();
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }
}
